/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.requirement.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.modules.policy.domain.dto.PolicyDto;
import com.fzhucc.modules.requirement.domain.Requirement;
import com.fzhucc.modules.requirement.domain.dto.RequirementDto;
import com.fzhucc.modules.requirement.service.RequirementService;
import com.fzhucc.modules.requirement.domain.dto.RequirementQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-02-28
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "智库:技术需求管理")
@RequestMapping("/api/requirement")
public class RequirementController {

    private final RequirementService requirementService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('requirement:list')")
    public void exportRequirement(HttpServletResponse response, RequirementQueryCriteria criteria) throws IOException {
        requirementService.download(requirementService.queryAll(criteria), response);
    }

    @AnonymousGetMapping("/show")
    @Log("查询技术需求管理")
    @ApiOperation("查询技术需求管理")
    /*@PreAuthorize("@el.check('requirement:list')")*/
    public ResponseEntity<Object> queryRequirement(RequirementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(requirementService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增技术需求管理")
    @ApiOperation("新增技术需求管理")
    @PreAuthorize("@el.check('requirement:add')")
    public ResponseEntity<Object> createRequirement(@Validated @RequestBody Requirement resources){
        return new ResponseEntity<>(requirementService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改技术需求管理")
    @ApiOperation("修改技术需求管理")
    @PreAuthorize("@el.check('requirement:edit')")
    public ResponseEntity<Object> updateRequirement(@Validated @RequestBody Requirement resources){
        requirementService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除技术需求管理")
    @ApiOperation("删除技术需求管理")
    @PreAuthorize("@el.check('requirement:del')")
    public ResponseEntity<Object> deleteRequirement(@RequestBody Integer[] ids) {
        requirementService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个技术需求")
    @ApiOperation("查询单个技术需求")
    public RequirementDto queryRequirementById(@PathVariable Integer id) {
        return requirementService.findById(id);
    }
}